package com.aynu.admin.service.base.impl;

import com.aynu.admin.domain.CommonResponse;
import com.aynu.admin.domain.po.TAdmin;
import com.aynu.admin.mapper.base.TAdminMapper;
import com.aynu.admin.service.base.ITAdminService;
import com.aynu.admin.utils.admin.JWTTool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-02-26
 */
@Service
public class TAdminServiceImpl extends ServiceImpl<TAdminMapper, TAdmin> implements ITAdminService {
    @Resource
    private TAdminMapper adminMapper;
    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private JWTTool jwtTool;

    @Override
    public CommonResponse<String> login(String username, String password) {
        TAdmin tAdmin = adminMapper.selectOne(
                Wrappers
                        .<TAdmin>lambdaQuery()
                        .eq(TAdmin::getUsername, username)
        );
        if (tAdmin == null) {
            return CommonResponse.failed("用户不存在");
        }
        if (!passwordEncoder.matches(password, tAdmin.getPassword())) {
            return CommonResponse.failed("密码错误");
        }
        String token = jwtTool.createToken(username);
        return CommonResponse.success(token);
    }

    @Override
    public CommonResponse<Object> register(TAdmin admin) {
//        判断是否存在
        TAdmin tAdmin = adminMapper.selectOne(Wrappers
                .<TAdmin>lambdaQuery()
                .eq(TAdmin::getUsername, admin.getUsername()));
        if (tAdmin != null) {
            return CommonResponse.failed("该管理员已存在");
        }
        //插入新管理员
        admin.setPassword(passwordEncoder.encode(admin.getPassword()))
                .setCreatedBy("super_admin")
                .setCreatedTime(LocalDateTime.now());
        adminMapper.insert(admin);
        return CommonResponse.SUCCESS;
    }

    @Override
    public CommonResponse<Object> modifyPassword(String username, String newPassword) {
        adminMapper.update(
                Wrappers
                        .<TAdmin>lambdaUpdate()
                        .eq(TAdmin::getUsername, username)
                        .set(TAdmin::getPassword, passwordEncoder.encode(newPassword))
        );
        return CommonResponse.SUCCESS;
    }

    @Override
    public CommonResponse<Object> deleteByUsername(String username, String deleteUsername) {
        if(!"super_admin".equals(username)){
            return CommonResponse.failed("没有权限");
        }
        if("super_admin".equals(deleteUsername)){
            return CommonResponse.failed("不能删除系统默认管理员");
        }
        int remove = adminMapper.delete(
                Wrappers
                        .<TAdmin>lambdaQuery()
                        .eq(TAdmin::getUsername, deleteUsername)
        );
        if(remove==1){
            return CommonResponse.SUCCESS;
        }
        return CommonResponse.failed("发生错误");
    }
}
